package com.example.stompdemo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import java.security.Principal;

@Controller
public class WebSocketController {

    private static final String DEFAULT_NAME = "fgz";
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping("/chat")
    public void handleChat(Principal principal,String msg) {
        if(DEFAULT_NAME.equals(principal.getName())){
            simpMessagingTemplate.convertAndSendToUser("zzz","/queue/notifications",
                    principal.getName() + "-发送:" + msg);
        } else {
            simpMessagingTemplate.convertAndSendToUser(DEFAULT_NAME,"/queue/notifications",
                    principal.getName() + "-发送:" + msg);
        }
    }
    @MessageMapping("/topic")
    public void gettopicchat(Principal principal,String msg) {
        System.out.println("连接topic");
       simpMessagingTemplate.convertAndSend("/topic/getResponse","我不爱你");
    }
}
